<template>
  <div class="container" @scroll="scrollArt" ref="box">
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="detail">
      <h3 class="title">{{ art_details.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="art_details.aut_photo"
        />
        <div class="text">
          <p class="name">{{ art_details.aut_name }}</p>
          <p class="time">{{ art_details.pubdate | dateFrom }}</p>
        </div>
        <van-button round size="small" type="info" @click="toggleFollow">{{
          art_details.is_followed ? "已关注" : "+ 关注"
        }}</van-button>
      </div>
      <div class="content" v-html="art_details.content"></div>
      <div class="zan">
        <!-- 1喜欢 0不喜欢 -1无态度 -->
        <van-button
          @click="changeAttitude(1)"
          round
          size="small"
          :class="{ active: art_details.attitude === 1 }"
          plain
          icon="like-o"
          >点赞</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          @click="changeAttitude(0)"
          round
          size="small"
          plain
          icon="delete"
          :class="{ active: art_details.attitude === 0 }"
          >不喜欢</van-button
        >
      </div>
    </div>
    <Comments />
  </div>
</template>

<script>
import { getArticelDetailApi } from '@/api/article'
import { follow, unFollowed, like, unLike, dislike, unDislike } from '@/api/user'

import Comments from './components/comments.vue'

export default {
  name: 'articleDetails',
  components: {
    Comments
  },
  data () {
    return {
      timer: 0,
      art_details: {},
      scrollTop: 0
    }
  },
  activated () {
    if (this.art_details.art_id !== this.$route.params.id) {
      this.scrollTop = 0
      this.getArticelDetail()
    } else {
      this.scrollTop && (this.$refs.box.scrollTop = this.scrollTop)
    }
  },
  methods: {
    async changeAttitude (type) {
      // 当点喜欢时
      if (type === 1) {
        if (this.art_details.attitude === 1) {
          // 取消喜欢
          this.art_details.attitude = -1
          await unLike(this.art_details.art_id)
        } else {
          // 喜欢
          await like(this.art_details.art_id)
          this.art_details.attitude = 1
        }
      } else {
        // 当点不喜欢时
        if (this.art_details.attitude === 0) {
          // 取消不喜欢
          this.art_details.attitude = -1
          await unDislike(this.art_details.art_id)
        } else {
          // 不喜欢
          this.art_details.attitude = 0
          await dislike(this.art_details.art_id)
        }
      }
    },
    async toggleFollow () {
      console.log(this.art_details.aut_id)
      try {
        // 取关
        if (this.art_details.is_followed) {
          const res = await unFollowed(this.art_details.aut_id)
          this.art_details.is_followed = false
          console.log(res)
        } else {
          // 关注
          const res = await follow(this.art_details.aut_id)
          this.art_details.is_followed = true
          console.log(res)
        }
      } catch (error) {
        console.log(error)
      }
    },
    scrollArt (e) {
      this.timer && clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.scrollTop = e.target.scrollTop
      }, 500)
    },
    async getArticelDetail () {
      try {
        const { data } = await getArticelDetailApi(this.$route.params.id)
        console.log(this.$route.params.id)
        console.log(data)
        this.art_details = data
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.detail {
  padding: 46px 10px 44px;
  // height: 1000%;
  .title {
    font-size: 18px;
    line-height: 2;
  }
  .zan {
    text-align: center;
    padding: 10px 0;
    .active {
      border-color: red;
      color: red;
    }
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.5;
      text-align: left;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  // 内容中包含：img 特别宽  code pre 不能换行
  .content {
    padding: 20px 0;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
    /deep/ code {
      white-space: pre-wrap;
    }
    /deep/ pre {
      white-space: pre-wrap;
    }
  }
}
</style>
